package com.deve.pig.controller.merchant;

import com.deve.pig.controller.BaseController;
import com.deve.pig.model.Merchant;
import com.deve.pig.model.MerchantGoods;
import com.deve.pig.model.MerchantImages;
import com.deve.pig.model.MerchantImagesExample;
import com.deve.pig.service.merchant.IMerchantImagesService;
import com.deve.pig.service.merchant.IMerchantService;
import my.utils.DwzAjaxJsonUtil;
import my.utils.Pagination;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @author tanyouzhong
 * @description 商家图片控制器
 * @date 2014年9月21日 下午15:21:21
 */
@Controller
@RequestMapping("/merchant/images")
public class MerchantImagesController extends BaseController {

    @Resource
    private IMerchantImagesService merchantImagesService;

    @Resource
    private IMerchantService merchantService;

    @RequestMapping("/showList")
    public String shownList(Model model, HttpServletRequest request, HttpServletResponse response) {

        Map<String, Object> params = super.getRequestParameters(request);
        int pageNum = Pagination.CURRENTPAGE;
        int pageSize = Pagination.PAGESIZE;
        if (params.containsKey("pageNum")) {
            pageNum = Integer.parseInt((String) params.get("pageNum"));
        }
        if (params.containsKey("numPerPage")) {
            pageSize = Integer.parseInt((String) params.get("numPerPage"));
        }
        Pagination page = merchantImagesService.findPagination(params, pageNum, pageSize);

        model.addAttribute("page", page);

        return "admin/merchant/images/imageslist";
    }


    @RequestMapping("/showAdd")
    public String showAdd(Model model) {
        return "admin/merchant/images/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String, Object> add(MerchantImages images,
                                   HttpServletRequest request) throws IOException {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        if (images == null) {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        } else {
            if (images.getIsCover() == null) {
                images.setIsCover(Byte.valueOf("0"));
            } else {
                // 取消已有的封面设置
                MerchantImagesExample mie = new MerchantImagesExample();
                mie.createCriteria().andMerchantIdEqualTo(images.getMerchantId()).andIsCoverEqualTo(Byte.valueOf("1"));
                merchantImagesService.updateByExampleSelective(new MerchantImages(Byte.valueOf("0")), mie);
                // 更新商家表封面冗余字段
                merchantService.update(new Merchant(images.getMerchantId(), images.getImage(), new Date()));
            }
            if (merchantImagesService.add(images) < 1) {
                json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
                json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
            }
        }
        return json;
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") int id, HttpServletRequest request) throws IOException {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");

        if (merchantImagesService.delete(id) < 1) {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "删除失败！");
        }
        return json;
    }

    @RequestMapping("/showUpdate/{id}")
    public String showUpdate(@PathVariable("id") int id, Model model) {
        MerchantImages images = merchantImagesService.findById(id);
        model.addAttribute("images", images);
        return "admin/merchant/images/update";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(MerchantImages images,
                                      HttpServletRequest request) throws IOException {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        if (images == null) {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        } else {
            if (images.getIsCover() == null) {
                images.setIsCover(Byte.valueOf("0"));
            } else {
                // 取消已有的封面设置
                MerchantImagesExample mie = new MerchantImagesExample();
                mie.createCriteria().andMerchantIdEqualTo(images.getMerchantId()).andIsCoverEqualTo(Byte.valueOf("1"));
                merchantImagesService.updateByExampleSelective(new MerchantImages(Byte.valueOf("0")), mie);
                // 更新商家表封面冗余字段
                merchantService.update(new Merchant(images.getMerchantId(), images.getImage(), new Date()));
            }
            if (merchantImagesService.update(images) < 1) {
                json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
                json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
            }
        }
        return json;
    }

    /**
     * @Description: 设置封面
     */
    @RequestMapping("/cover")
    @ResponseBody
    public Map<String, Object> cover(MerchantImages images, HttpServletRequest request) {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
        if (images == null) {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        } else {
            // 取消已有的封面设置
            MerchantImagesExample mie = new MerchantImagesExample();
            mie.createCriteria().andMerchantIdEqualTo(images.getMerchantId()).andIsCoverEqualTo(Byte.valueOf("1"));
            merchantImagesService.updateByExampleSelective(new MerchantImages(Byte.valueOf("0")), mie);
            // 重新设置封面
            images.setIsCover(Byte.valueOf("1"));
            merchantImagesService.update(images);
            // 更新商家表封面冗余字段
            String coverUrl = merchantImagesService.findById(images.getId()).getImage();
            merchantService.update(new Merchant(images.getMerchantId(), coverUrl, new Date()));
        }
        return json;
    }

    /**
     * @Description: 取消设置封面
     */
    @RequestMapping("/unCover")
    @ResponseBody
    public Map<String, Object> unCover(MerchantImages images, HttpServletRequest request) {
        Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
        json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
        json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");
        if (images == null) {
            json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
            json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
        } else {
            images.setIsCover(Byte.valueOf("0"));
            merchantImagesService.update(images);
        }
        return json;
    }

    /**
     * @description 查看大图
     */
    @RequestMapping("/image")
    public String detail() throws IOException {
        return "admin/merchant/images/image";
    }
}
